/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.ProfileManager.profile;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Professional extends Profile {
	public int education, degree, industry, subInd;

	public String highSchool, college, major, occupation, company, companyWeb,
			title, jobDesc, workemail, workphone, careerSkills, careerInt;

	public int year;

	public String toXML() {
		StringBuilder output = new StringBuilder();
		output
				.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources>");
		output.append("<item name=\"subInd\">" + subInd + "</item>");
		output.append("<item name=\"industry\">" + industry + "</item>");
		output.append("<item name=\"degree\">" + degree + "</item>");
		output.append("<item name=\"education\">" + education + "</item>");
		output.append("<item name=\"year\">" + year + "</item>");
		output.append("<item name=\"careerInt\">" + careerInt + "</item>");
		output.append("<item name=\"careerSkills\">" + careerSkills
				+ "</item>");
		output.append("<item name=\"workphone\">" + workphone + "</item>");
		output.append("<item name=\"workemail\">" + workemail + "</item>");
		output.append("<item name=\"jobDesc\">" + jobDesc + "</item>");
		output.append("<item name=\"title\">" + title + "</item>");
		output.append("<item name=\"companyWeb\">" + companyWeb + "</item>");
		output.append("<item name=\"company\">" + company + "</item>");
		output.append("<item name=\"occupation\">" + occupation + "</item>");
		output.append("<item name=\"major\">" + major + "</item>");
		output.append("<item name=\"college\">" + college + "</item>");
		output.append("<item name=\"highSchool\">" + highSchool + "</item>");
		output.append("</resources>");
		
		/*StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i< output.length(); i++){
			char c;
			if((c = output.charAt(i))==' '){
				sb.append("%20");
			}else{
				sb.append(c);
			}
		}*/
		return output.toString();
	}

	public void fromXML(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	class MyHandler extends DefaultHandler {
		String lastElement;

		boolean begin;

		public void endElement(String uri, String localName, String qName) {
			begin = false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin = true;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
			}
		}

		public void characters(char[] ch, int start, int length) {
			String s = new String(ch, start, length);
			if(s.compareTo("null")==0)return;
			if (lastElement == null)
				return;
			if (!begin)
				return;
			if (lastElement.compareTo("education") == 0) {
				Professional.this.education = Integer.parseInt(s);
			} else if (lastElement.compareTo("degree") == 0) {
				Professional.this.degree = Integer.parseInt(s);
			} else if (lastElement.compareTo("industry") == 0) {
				Professional.this.industry = Integer.parseInt(s);
			} else if (lastElement.compareTo("subInd") == 0) {
				Professional.this.subInd = Integer.parseInt(s);
			} else if (lastElement.compareTo("highSchool") == 0) {
				Professional.this.highSchool = s;
			} else if (lastElement.compareTo("college") == 0) {
				Professional.this.college = s;
			} else if (lastElement.compareTo("major") == 0) {
				Professional.this.major = s;
			} else if (lastElement.compareTo("occupation") == 0) {
				Professional.this.occupation = s;
			} else if (lastElement.compareTo("company") == 0) {
				Professional.this.company = s;
			} else if (lastElement.compareTo("companyWeb") == 0) {
				Professional.this.companyWeb = s;
			} else if (lastElement.compareTo("title") == 0) {
				Professional.this.title = s;
			} else if (lastElement.compareTo("jobDesc") == 0) {
				Professional.this.jobDesc = s;
			} else if (lastElement.compareTo("workemail") == 0) {
				Professional.this.workemail = s;
			} else if (lastElement.compareTo("workphone") == 0) {
				Professional.this.workphone = s;
			} else if (lastElement.compareTo("jobDesc") == 0) {
				Professional.this.jobDesc = s;
			} else if (lastElement.compareTo("careerSkills") == 0) {
				Professional.this.careerSkills = s;
			} else if (lastElement.compareTo("careerInt") == 0) {
				Professional.this.careerInt = s;
			} else if (lastElement.compareTo("year") == 0) {
				Professional.this.year = Integer.parseInt(s);
			}

		}
	}

}
